package com.leetcode.LC;

import java.util.Stack;

public class LC1544 {
    public String makeGood(String s) {
        if (s.length()<=1){
            return s;
        }
        //java：利用栈的后进先出，如果当前字符与栈顶(如果存在)互为大小写，则将栈顶弹出，否则将当前字符入栈
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (stack.size() == 0) {
                stack.push(s.charAt(i));
            } else {
                if (stack.peek() == s.charAt(i) + ('A' - 'a') || stack.peek() == s.charAt(i) - ('A' - 'a')) {
                    stack.pop();
                }else {
                    stack.push(s.charAt(i));
                }
            }
        }
        StringBuilder builder = new StringBuilder();
        while (stack.size() > 0) {
            builder.append(stack.pop());
        }
        return builder.reverse().toString();
    }
}
